package com.erp.order.purchasing.controller;

import com.erp.hr.base.controller.BaseController;
import com.erp.hr.base.util.CommonUtils;
import com.erp.hr.base.vo.ResultVO;
import com.erp.order.purchasing.model.PurchasingRequisition;
import com.erp.order.purchasing.service.PurchasingRequisitionService;
import org.redragon.framework.hibernate.model.Pages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 采购申请 控制层
 * @Author: syz
 * @blame syz
 * @CreateTime: 2021/4/6
 */
@Controller
@RequestMapping("web/purchasingRequisition")
public class PurchasingRequisitionController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PurchasingRequisitionController.class);

    /**
     * 采购申请服务
     */
    @Autowired
    private PurchasingRequisitionService purchasingRequisitionService;


    /**
     * 获取列表分页数据
     * @param pages 分页对象
     * @param purchasingRequisition 采购申请对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("getPageList")
    public String getPageList(Pages pages, PurchasingRequisition purchasingRequisition, Model model) {
        model.addAttribute("pages", setPages(pages));
        model.addAttribute("list", purchasingRequisitionService.addCompleteData(purchasingRequisitionService.getDataObjectsForDataAuth("", pages, purchasingRequisition)));
        model.addAttribute("typeMap", purchasingRequisitionService.getDictByKey("requisition_type"));
        model.addAttribute("statusMap", purchasingRequisitionService.getDictByKey("requisition_status"));
        return "basic.jsp?content=purchasingRequisition/list";
    }

    /**
     * 获取新增/编辑数据
     * @param purchasingRequisition 新增/编辑对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("get")
    public String get(PurchasingRequisition purchasingRequisition, Model model) {
        if(purchasingRequisition != null && purchasingRequisition.getId() != null) {
            model.addAttribute("purchasingRequisition",
                purchasingRequisitionService.addCompleteData(
                        purchasingRequisitionService.getDataObject(
                                purchasingRequisition.getId().toString())));
        }else {
            purchasingRequisitionService.initCreateInfo(purchasingRequisition);
            model.addAttribute("purchasingRequisition", purchasingRequisition);
        }
        model.addAttribute("typeMap", purchasingRequisitionService.getDictByKey("requisition_type"));
        model.addAttribute("unitMap", purchasingRequisitionService.getDictByKey("material_unit"));
        model.addAttribute("subStatusMap", purchasingRequisitionService.getDictByKey("requisition_detail_status"));
        return "basic.jsp?content=purchasingRequisition/edit";
    }

    /**
     * 新增/编辑
     * @param purchasingRequisition 需要新增/编辑的对象
     * @return 新增/编辑结果
     */
    @ResponseBody
    @RequestMapping("save")
    public ResultVO<String> save(@Valid @RequestBody PurchasingRequisition purchasingRequisition) {
        return saveOrUpdate(purchasingRequisitionService, purchasingRequisition);
    }

    /**
     * 提交
     * @param id 需要提交对象的id
     * @return 提交结果
     */
    @ResponseBody
    @RequestMapping("submit")
    public ResultVO<String> submit(Long id) {
        return purchasingRequisitionService.submit(id);
    }

    /**
     * 撤回
     * @param id 需要撤回对象的id
     * @return 撤回结果
     */
    @ResponseBody
    @RequestMapping("recall")
    public ResultVO<String> recall(Long id) {
        return purchasingRequisitionService.recall(id);
    }

    /**
     * 更新申请为不可退回
     * @param purchasingRequisition 需要更新的对象
     * @return 更新结果
     */
    @ResponseBody
    @RequestMapping("updateAllowedWithdraw")
    public ResultVO<String> updateAllowedWithdraw(@RequestBody PurchasingRequisition purchasingRequisition) {
        PurchasingRequisition dataObject = purchasingRequisitionService.getDataObject(purchasingRequisition.getId().toString());
        dataObject.setAllowedWithdraw(CommonUtils.IS_DEL_NO);
        purchasingRequisitionService.updateDataObject(dataObject);
        return ResultVO.success();
    }

    /**
     * 跳转至详情页
     * @param purchasingRequisition 采购申请对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("detail")
    public String detail(PurchasingRequisition purchasingRequisition, Model model) {
        model.addAttribute("purchasingRequisition",
                purchasingRequisitionService.addCompleteData(
                        purchasingRequisitionService.getDataObject(
                                purchasingRequisition.getId().toString())));
        model.addAttribute("typeMap", purchasingRequisitionService.getDictByKey("requisition_type"));
        model.addAttribute("unitMap", purchasingRequisitionService.getDictByKey("material_unit"));
        model.addAttribute("subStatusMap", purchasingRequisitionService.getDictByKey("requisition_detail_status"));
        return "basic.jsp?content=purchasingRequisition/detail";
    }

    /**
     * 删除
     * @param id 需要删除对象的id
     * @return 删除结果
     */
    @ResponseBody
    @RequestMapping("del")
    public ResultVO<String> del(Long id) {
        return linkDelete(purchasingRequisitionService, id);
    }

    /**
     * 附件上传
     * @param request 请求对象
     * @param files 附件
     * @return 文件vo
     */
    @ResponseBody
    @RequestMapping("upload")
    public List upload(@RequestParam("accessory") List<MultipartFile> files, HttpServletRequest request) {
        return fileUpload(request, files);
    }

}